import Cookies from 'js-cookie'

const TokenKey = 'token'

export function getCookie(key) {
  if (Cookies.get(key) && Cookies.get(key) !== 'undefined' && Cookies.get(key) !== 'null') {
    return Cookies.get(key)
  } else {
    return ''
  }
  return Cookies.get(key)
}

export function setCookie(key, value) {
  return Cookies.set(key, value)
}

export function removeCookie(key) {
  if (key) {
    return Cookies.remove(key)
  } else {
    let keys = document.cookie.match(/[^ =;]+(?=\=)/g)
    keys.forEach((item, index) => {
      Cookies.remove(item)
    })
    return;
  }
}


export function getToken() {
  return localStorage.getItem(TokenKey)
  // return Cookies.get(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
  // return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return localStorage.removeItem(TokenKey)
  // return Cookies.remove(TokenKey)
}

/**
 * sessionStorage
 * 获取  为"key" 的值
 * @param key
 * @returns {string}
 */
export function getSession(key) {
  if (sessionStorage.getItem(key) && sessionStorage.getItem(key) !== 'undefined' && sessionStorage.getItem(key) !== 'null') {
    return sessionStorage.getItem(key)
  } else {
    return ''
  }
}

/**
 * 设置 为"key" 的值
 * @param key
 * @param data
 */

export function setSession(key, data) {
  if (typeof data === 'object') {
    return sessionStorage.setItem(key, JSON.stringify(data))
  } else {
    return sessionStorage.setItem(key, data)
  }
}

/**
 * 删除 为"key" 的值
 * @param key
 */
export function removeSession(key) {
  return sessionStorage.removeItem(TokenKey)
  // return Cookies.remove(TokenKey)
}

/**
 * 清空sessionStorage中所有信息
 */
export function clearSession() {
  return sessionStorage.clear()
}